package com.fanling.venus.web.controller.project;

import com.fanling.venus.common.base.AjaxResult;
import com.fanling.venus.frame.web.BaseController;
import com.fanling.venus.frame.web.TableDataInfo;
import com.fanling.venus.project.domain.EpointWeeklyWork;
import com.fanling.venus.project.service.EpointWeeklyWeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/project/epointweeklywork")
public class EpointWeeklyWorkController extends BaseController {

    private String prefix = "project/epointweeklywork";

    @Autowired
    private EpointWeeklyWeekService epointWeeklyWeekService;

    /**
     * 页面跳转
     *
     * @return
     */
    @GetMapping()
    public String weeklywork() {
        return prefix + "/epointweeklywork";
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/epointweeklywork-add";
    }

    @GetMapping("/edit/{weekId}")
    public String edit(@PathVariable("weekId") Long weekId, ModelMap mmap) {
        mmap.put("epointweeklywork", epointWeeklyWeekService.selectWeeklyWorkById(weekId));
        return prefix + "/epointweeklywork-edit";
    }

    /**
     * 展示列表
     *
     * @param epointWeeklyWork
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EpointWeeklyWork epointWeeklyWork) {
        startPage();
        List<EpointWeeklyWork> list = epointWeeklyWeekService.selectWeeklyWorkList(epointWeeklyWork);
        return getDataTable(list);
    }

    /**
     * 新增任务
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EpointWeeklyWork epointWeeklyWork) {
        String uniqueFlag = epointWeeklyWeekService.checkProjectNameAndWorkTitleUnique(epointWeeklyWork);
        if ("1".equals(uniqueFlag)) {
            return error(1, "类似工作已经存在，检查是否必要新增！");
        }
        return toAjax(epointWeeklyWeekService.insertWeeklyWork(epointWeeklyWork));
    }

    /**
     * 修改任务
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EpointWeeklyWork epointWeeklyWork) {
        return toAjax(epointWeeklyWeekService.updateWeeklyWork(epointWeeklyWork));
    }

    /**
     * 删除任务
     *
     * @param workId
     * @return
     */
    @PostMapping("/remove/{workId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("workId") Long workId) {
        return toAjax(epointWeeklyWeekService.deleteWeeklyWorkById(workId));
    }
}
